package com.hx.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.hx.common.utils.DateUtils;
import com.hx.order.domain.OrderItem;
import com.hx.order.domain.Orders;
import com.hx.order.mapper.OrderItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hx.order.service.IOrderItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单商品详情Service业务层处理
 *
 * @author ord
 * @date 2023-05-29
 */
@Service
public class OrderItemServiceImpl implements IOrderItemService {
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrdersServiceImpl ordersService;

    /**
     * 查询订单商品详情
     *
     * @param id 订单商品详情主键
     * @return 订单商品详情
     */
    @Override
    public OrderItem selectOrderItemById(Long id)
    {
        return orderItemMapper.selectOrderItemById(id);
    }

    /**
     * 查询订单商品详情列表
     *
     * @param orderItem 订单商品详情
     * @return 订单商品详情
     */
    @Override
    public List<OrderItem> selectOrderItemList(OrderItem orderItem)
    {
        return orderItemMapper.selectOrderItemList(orderItem);
    }

    /**
     * 新增订单商品详情
     *
     * @param orderItem 订单商品详情
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOrderItem(OrderItem orderItem)
    {
        Long userId = orderItem.getUserId();
        BigDecimal price = orderItem.getPrice();
        orderItem.setUserId(userId);
        System.out.println(userId);
        String uuid = DateUtils.dateTimeNow()+System.currentTimeMillis();
        orderItem.setOrderSn(uuid);
        Orders orders = new Orders();
        orders.setOrderSn(uuid);
        orders.setUserId(userId);
        orders.setTotalAmount(price);
        ordersService.insertOrders(orders);
        return orderItemMapper.insertOrderItem(orderItem);
    }

    /**
     * 修改订单商品详情
     *
     * @param orderItem 订单商品详情
     * @return 结果
     */
    @Override
    public int updateOrderItem(OrderItem orderItem)
    {
        return orderItemMapper.updateOrderItem(orderItem);
    }

    /**
     * 批量删除订单商品详情
     *
     * @param ids 需要删除的订单商品详情主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemByIds(Long[] ids)
    {
        return orderItemMapper.deleteOrderItemByIds(ids);
    }

    /**
     * 删除订单商品详情信息
     *
     * @param id 订单商品详情主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemById(Long id)
    {
        return orderItemMapper.deleteOrderItemById(id);
    }
}
